/*
This file is part of vlc-remote.

vlc-remote is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

vlc-remote is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with vlc-remote.  If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 1.1
import com.meego 1.0
import "common"
import "mediaRemoteFunctions.js" as MediaRemote
import "remoteStatus.js" as RemoteStatus
import "vlcRemote.js" as VlcRemote

Page {
    id: settingsPage
    property string server: ""
    property string serverName: "Not connected"

    property color accentColor: "#f78500"
    property int coverWidth: 480
    property int coverHeight: 480
    property alias autoloadLastConnection:  chkAutoload.checked
    property int playerRefreshFocused: 5000 //playerFocusedSelectionDialog.model.get(playerFocusedSelectionDialog.selectedIndex).value
    property int playerRefreshThumbnailed: 120000 //playerThumbnailedSelectionDialog.model.get(playerThumbnailedSelectionDialog.selectedIndex).value
    property int playlistRefreshFocused: 600000 //playlistFocusedSelectionDialog.model.get(playlistFocusedSelectionDialog.selectedIndex).value
    property int playlistRefreshThumbnailed: 1200000 //playlistThumbnailedSelectionDialog.model.get(playlistThumbnailedSelectionDialog.selectedIndex).value

    tools: menuTools

    Component.onCompleted: {
        screen.allowedOrientations = Screen.Portrait;
        //lockInPortrait = true;
        orientationLock = Screen.Portrait;

        load();
    }

    onVisibleChanged: {
        load();
    }

    // JS need to be inside a JS file..
    function save()
    {
    }

    function load()
    {
        //name TEXT, type TEXT, ipAddress TEXT, port TEXT, username TEXT, password TEXT, secured BOOLEAN
        var globalSettings = MediaRemote.loadSettingsByConnection("");
        for (var i = 0; i < globalSettings.length; ++i) {
            var name = globalSettings.item(i).name;
            var type = globalSettings.item(i).type;
            var intVal = Math.round(globalSettings.item(i).integerField);
            var textVal = globalSettings.item(i).textField;
            //console.debug("loading setting: " + name + " " + intVal);
            switch (name) {
            case "autoloadLastConnection":
                autoloadLastConnection = (intVal != 0);
                break;
            case "playerRefreshFocused":
                playerRefreshFocused = intVal;
                break;
            case "playerRefreshThumbnailed":
                playerRefreshThumbnailed = intVal;
                break;
            case "playlistRefreshFocused":
                playlistRefreshFocused = intVal;
                break;
            case "playlistRefreshThumbnailed":
                playlistRefreshThumbnailed = intVal;
                break;
            }
        }
        playerFocusedSelectionDialog.selectedIndex = secondsList.getIndexFromValue(playerRefreshFocused);
        playerThumbnailedSelectionDialog.selectedIndex = secondsList.getIndexFromValue(playerRefreshThumbnailed);
        playlistFocusedSelectionDialog.selectedIndex = secondsList.getIndexFromValue(playlistRefreshFocused);
        playlistThumbnailedSelectionDialog.selectedIndex = secondsList.getIndexFromValue(playlistRefreshThumbnailed);

        if ("" != serverName) {
            var localSettings = MediaRemote.loadSettingsByConnection(serverName);
            for (var j = 0; i < localSettings.length; ++i) {

            }
        }

    }

    ButtonStyle {
        id: vlcButtonStyle
        textColor: "white"
        background: "black"
    }

    PageHeader {
        id: pageHeader
        anchors.top: settingsPage.top
        title: qsTr("Settings")
        subtitle: serverName + " @ " + server + ""
        onClicked: { myMenu.close(); pageStack.pop(); }
    }

    ListModel {
        id: secondsList
        function getIndexFromValue(v) {
            switch(v) {
            case 1000:
                return 0;
            case 2000:
                return 1;
            case 3000:
                return 2;
            case 4000:
                return 3;
            case 5000:
                return 4;
            case 10000:
                return 5;
            case 30000:
                return 6;
            case 60000:
                return 7;
            case 120000:
                return 8;
            case 180000:
                return 9;
            case 240000:
                return 10;
            case 300000:
                return 11;
            case 600000:
                return 12;
            case 1800000:
                return 13;
            case 3600000:
                return 14;
            default:
                return -1;
            }
        }
        ListElement { name: "1 second";   value: 1000;    }
        ListElement { name: "2 seconds";  value: 2000;    }
        ListElement { name: "3 seconds";  value: 3000;    }
        ListElement { name: "4 seconds";  value: 4000;    }
        ListElement { name: "5 seconds";  value: 5000;    }
        ListElement { name: "10 seconds"; value: 10000;   }
        ListElement { name: "30 seconds"; value: 30000;   }
        ListElement { name: "1 minute";   value: 60000;   }
        ListElement { name: "2 minutes";  value: 120000;  }
        ListElement { name: "3 minutes";  value: 180000;  }
        ListElement { name: "4 minutes";  value: 240000;  }
        ListElement { name: "5 minutes";  value: 300000;  }
        ListElement { name: "10 minutes"; value: 600000;  }
        ListElement { name: "30 minutes"; value: 1800000; }
        ListElement { name: "1 hour";     value: 3600000; }
    }

    SelectionDialog {
        id: playerFocusedSelectionDialog
        model: secondsList
        onSelectedIndexChanged: {
            var val = model.get(selectedIndex).value;
            MediaRemote.saveSetting("playerRefreshFocused", "", "int", "", val);
            playerRefreshFocused = val;
        }
        titleText: qsTr("Update frequency")
    }

    SelectionDialog {
        id: playerThumbnailedSelectionDialog
        model: secondsList
        onSelectedIndexChanged: {
            var val = model.get(selectedIndex).value;
            MediaRemote.saveSetting("playerRefreshThumbnailed", "", "int", "", val);
            playerRefreshThumbnailed = val;
        }
        titleText: qsTr("Update frequency")
    }

    SelectionDialog {
        id: playlistFocusedSelectionDialog
        model: secondsList
        onSelectedIndexChanged: {
            var val = model.get(selectedIndex).value;
            MediaRemote.saveSetting("playlistRefreshFocused", "", "int", "", val);
            playlistRefreshFocused = val;
        }
        titleText: qsTr("Update frequency")
    }

    SelectionDialog {
        id: playlistThumbnailedSelectionDialog
        model: secondsList
        onSelectedIndexChanged: {
            var val = model.get(selectedIndex).value;
            MediaRemote.saveSetting("playlistRefreshThumbnailed", "", "int", "", val);
            playlistRefreshThumbnailed = val;
        }
        titleText: qsTr("Update frequency")
    }

    Flickable {
        id: settingsPane;
        anchors.fill: parent
        anchors.topMargin: 58
        contentWidth: aboutPage.width
        clip:  true
        flickableDirection: Flickable.VerticalFlick

        Column {
            anchors.fill: parent
            spacing: 16

            Row {

                spacing:  16
                anchors.margins: 12

                Text {
                    id: lblAutoload
                    width: settingsPage.width - 120
                    anchors.margins: 12
                    maximumLineCount: 3
                    color: "white"
                    font.pixelSize: 24
                    wrapMode: "WrapAtWordBoundaryOrAnywhere"
                    text: qsTr("Open last used connection when application starts?")
                }

                Switch {
                    id: chkAutoload
                    width: 120
                    anchors.margins: 8
                    checked: false //autoloadLastConnection
                    onCheckedChanged: {
                        MediaRemote.saveSetting("autoloadLastConnection", "", "bool", "", (checked) ? 1 : 0);
                    }
                }
            }

            Text {
                id: lblPlayerFocused
                width: settingsPage.width
                anchors.margins: 12
                maximumLineCount: 3
                color: "white"
                font.pixelSize: 24
                wrapMode: "WrapAtWordBoundaryOrAnywhere"
                text: qsTr("How frequently to refresh the player when focused.")
            }

            Button {
                id: btnPlayerFocused
                width: 400
                text: qsTr("Every " + playerFocusedSelectionDialog.model.get(playerFocusedSelectionDialog.selectedIndex).name)
                onClicked: playerFocusedSelectionDialog.open()
            }

            Text {
                id: lblPlayerFocusThumbnailed
                width: settingsPage.width
                anchors.margins: 12
                maximumLineCount: 3
                color: "white"
                font.pixelSize: 24
                wrapMode: "WrapAtWordBoundaryOrAnywhere"
                text: qsTr("How frequently to refresh the player when thumbnailed.")
            }
            Button {
                id: btnPlayerThumbnailed
                width: 400
                text: qsTr("Every " + playerThumbnailedSelectionDialog.model.get(playerThumbnailedSelectionDialog.selectedIndex).name)
                onClicked: playerThumbnailedSelectionDialog.open()
            }

            Text {
                id: lblPlaylistFocused
                width: settingsPage.width
                anchors.margins: 12
                maximumLineCount: 3
                color: "white"
                font.pixelSize: 24
                wrapMode: "WrapAtWordBoundaryOrAnywhere"
                text: qsTr("How frequently to refresh the playlist when focused.")
            }
            Button {
                id: btnPlaylistFocused
                width: 400
                text: qsTr("Every " + playlistFocusedSelectionDialog.model.get(playlistFocusedSelectionDialog.selectedIndex).name)
                onClicked: playlistFocusedSelectionDialog.open()
            }

            Text {
                id: lblPlaylistThumbnailed
                width: settingsPage.width
                anchors.margins: 12
                maximumLineCount: 3
                color: "white"
                font.pixelSize: 24
                wrapMode: "WrapAtWordBoundaryOrAnywhere"
                text: qsTr("How frequently to refresh the playlist when minimised.")
            }
            Button {
                id: btnPlaylistThumbnailed
                width: 400
                text: qsTr("Every " + playlistThumbnailedSelectionDialog.model.get(playlistThumbnailedSelectionDialog.selectedIndex).name)
                onClicked: playlistThumbnailedSelectionDialog.open()
            }


        }

    }

}
